<!--
* description: 交付地管理---交付地变总部店审核
* author:xlc
* createdDate: 2020-12-3
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
    <edit
      :key="editPopupsKey"
      :popups-visible="editPopupsVisible"
      :data-for-form="editRowData"
      :popups-state="editPopupsState"
      :title-name="titleName"
      @close="closeEdit"
    />
    <edit-emp
      :key="editEmpPopupsKey"
      :popups-visible="editEmpPopupsVisible"
      :data-for-form="editEmpRowData"
      :popups-state="editPopupsState"
      :title-name="titleName"
      @close="closeEdit"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import Edit from './edit'
import editEmp from './editEmp'
import { orgApis } from '@/api/vesalesapi.js'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
export default {
  name: 'DeliveryPlaceChangeHQ',
  components: {
    Edit,
    editEmp,
    OneTableTemplate
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'supplierMessage-edit'], () => {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      editEmpPopupsKey: '',
      editEmpRowData: {},
      editEmpPopupsVisible: false,
      titleName: '',
      // editRowData: {},
      // 网格查询API配置对象
      apiConfig: orgApis.deliveryPlaceChangeHQQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        }
        // {
        //   compKey: 'btnKey3',
        //   type: '',
        //   size: 'small',
        //   disabled: true,
        //   parentFileds: 'disabled:table',
        //   clickEvent: () => { this.save() },
        //   text: '通过',
        //   position: 'left'
        // }
        // {
        //   compKey: 'btnKey4',
        //   type: '',
        //   size: 'small',
        //   disabled: true,
        //   parentFileds: 'disabled:table',
        //   clickEvent: () => { this.reject(2) },
        //   text: '驳回',
        //   position: 'left'
        // }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '交付地变更申请单号：',
              codeField: 'jfdCode',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '关联订单号：',
              codeField: 'associatedOrder',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '申请日期：',
              codeField: 'applicationStartTime,applicationEndTime',
              component: () => import('@/components/org/datePicker/datePicker.vue'),
              dateType: 'daterange',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '申请人：',
              codeField: 'applicationName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '门店名称：',
              codeField: 'deliveryDlrId',
              textField: 'deliveryDlrShortName',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'deliveryDlrId,deliveryDlrShortName',
              customObj: {
                api: orgApis.ListOfDeliveryStore, selectType: '1'
              },
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '状态：',
              codeField: 'jfdStatus',
              lookuptype: 'VE1320',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              customObj: {
                listQuery: {
                  lookupTypeCode: 'VE1320',
                  attribute2: '1'
                }
              },
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '基础车型名称：',
              codeField: 'carConfigId',
              textField: 'carConfigCn',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carConfigId,carConfigCn',
              customObj: {
                api: orgApis.listOfCarModel, selectType: '1'
              },
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              width: 100,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '通过',
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: index => { this.showedit(index, '交付专员选择') },
                  rowFileds: 'show:jfdStatus-3',
                  component: () => import('@/components/org/linkButton')
                },
                {
                  compKey: 'propKey2',
                  labelName: '驳回',
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: index => { this.showedit(index, '驳回原因填写') },
                  rowFileds: 'show:jfdStatus-3',
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true
            },
            {
              prop: 'jfdCode',
              label: '交付地变更申请单号',
              width: 180
            },
            {
              prop: 'lookupValueName',
              label: '状态',
              width: 180
            },
            {
              prop: 'ZBAuditDate',
              label: '总部审核时间',
              width: 180
            },
            {
              prop: 'ZBRejectReason',
              label: '总部驳回原因',
              width: 180
            },
            {
              prop: 'dlrCode',
              label: '门店编码',
              width: 180
            },
            {
              prop: 'oldDlrShortName',
              label: '门店名称',
              width: 180
            },
            {
              prop: 'dlrAuditDate',
              label: '门店审核时间',
              width: 180
            },
            {
              prop: 'dlrRejectReason',
              label: '门店驳回原因',
              width: 180
            },
            {
              prop: 'applicationDate',
              label: '申请时间',
              width: 180
            },
            {
              prop: 'applicationName',
              label: '申请人',
              width: 180
            },
            {
              prop: 'jfdReason',
              label: '交付地变更原因',
              width: 180
            },
            {
              prop: 'oldDlrShortName',
              label: '原交付门店',
              width: 180
            },
            {
              prop: 'oldCarHouseName',
              label: '原交付仓库',
              width: 180
            },
            // 是否启用
            {
              prop: 'newDlrShortName',
              label: '变更后交付门店',
              width: 180
            },
            {
              prop: 'newCarHouseName',
              label: '变更后交付仓库',
              width: 180
            },
            {
              prop: 'associatedOrder',
              label: '关联订单号',
              width: 180
            },
            {
              prop: 'carType',
              label: '车型配置名称',
              width: 180
            }
          ],
      formField: {
        jfdCode: '',
        associatedOrder: '',
        deliveryDlrId: '',
        applicationStartTime: '',
        applicationEndTime: '',
        applicationName: '',
        jfdStatus: '',
        carConfigId: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      // 单表格模版页面组件回调事件
      callbackObj: {
        // onCheckSelectChange: (selection) => {
        //   this.selectList = selection
        //   this.passdata = this.selectList.reduce((pre, cur) => {
        //     pre.push({
        //       vin: cur.vin,
        //       assignNo: cur.assignNo,
        //       jfdCode: cur.jfdCode,
        //       updateControlId: cur.updateControlId,
        //       jfdNewHouseId: cur.jfdNewHouseId,
        //       jfdNewId: cur.jfdNewId,
        //       saleOrderId: cur.saleOrderId,
        //       newHouseCode: cur.newHouseCode,
        //       oldHouseCode: cur.oldHouseCode,
        //       dlrAnswerCode: cur.dlrAnswerCode,
        //       saleOrderSign: cur.saleOrderSign
        //     })
        //     return pre
        //   }, [])
        //   this.onCheckSelectChange1(selection)
        // },
        // // // 手动勾选时
        // onCheckSelect: (selection, row) => {
        //   this.editRowData = selection
        //   // // 清除所有选中
        //   // this.$refs.multipleTable.$refs.multipleTable.clearSelection()
        //   // if (selection.length === 0) return
        //   // // 将当前点击项选中
        //   // this.$refs.multipleTable.$refs.multipleTable.toggleRowSelection(row, true)
        // }
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  methods: {
    // 关闭弹窗
    closeEdit(type) {
      this.editEmpPopupsVisible = false
      this.editPopupsVisible = false
      if (type === '1') {
        this.queryTable()
      }
    },
    // 弹出
    showedit(index, name) {
      this.titleName = name
      // if (name === '交付专员选择' && this.$refs.multipleTable.list[index].saleOrderSign === '3') {
      //   this.save([this.$refs.multipleTable.list[index]])
      //   return
      // }
      if (name === '交付专员选择') {
        // this.editEmpRowData = this.$refs.multipleTable.list[index]
        // this.editEmpPopupsVisible = true
        // this.editEmpPopupsKey += '1'
        this.save([this.$refs.multipleTable.list[index]])
      } else {
        this.editRowData = this.$refs.multipleTable.list[index]
        this.editPopupsVisible = true
        this.editPopupsKey += '1'
      }
    },
    // 展车确认
    save(res) {
      const that = this
      const queryObj = {
        // api配置
        apiConfig: orgApis.deliveryPlaceChangeHQConfirm,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: { infoList: res }
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        this.$message({
          message: response.msg,
          duration: 2000
        })
        if (response.result === '1') {
          this.queryTable()
        }
      })
    },
    // 选择列是否生效
    getSelectable(row, index) {
      return (row.lookupValueName !== '总部审核通过' && row.lookupValueName !== '总部驳回')
    },
    onCheckSelectChange1(selection) {
      var btn1 = this.tableButtons[2]
      var btn2 = this.tableButtons[3]
      const isCheck = selection && selection.length > 0
      btn1.disabled = !isCheck
      btn2.disabled = !isCheck
    }
  }
}
</script>
